﻿using SqlSugar;
using System;

namespace WMS.Model
{
    [SugarTable("Menu")]
    public class MenuModel : BaseModel
    {
        /// <summary>
        /// 父级菜单ID，NULL表示根节点
        /// </summary>
        [SugarColumn(ColumnName = "ParentMenuId", IsNullable = true)]
        public long? ParentMenuId { get; set; } = null;

        /// <summary>
        /// 菜单显示名称
        /// </summary>
        [SugarColumn(ColumnName = "MenuName", Length = 50, IsNullable = false)]
        public string MenuName { get; set; }

        /// <summary>
        /// 菜单编码（唯一）
        /// </summary>
        [SugarColumn(ColumnName = "MenuCode", Length = 50, IsNullable = false)]
        public string MenuCode { get; set; }

        /// <summary>
        /// 菜单类型：1-目录 2-菜单 3-按钮
        /// </summary>
        [SugarColumn(ColumnName = "MenuType", DefaultValue = "1")]
        public byte MenuType { get; set; } = 1;

        /// <summary>
        /// 菜单路由路径
        /// </summary>
        [SugarColumn(ColumnName = "MenuPath", Length = 200, IsNullable = true)]
        public string MenuPath { get; set; }

        /// <summary>
        /// 菜单图标类名
        /// </summary>
        [SugarColumn(ColumnName = "MenuIcon", Length = 50, IsNullable = true)]
        public string? MenuIcon { get; set; }

        /// <summary>
        /// 前端组件名称
        /// </summary>
        [SugarColumn(ColumnName = "ComponentName", Length = 100, IsNullable = true)]
        public string ComponentName { get; set; }

        /// <summary>
        /// 排序序号（升序）
        /// </summary>
        [SugarColumn(ColumnName = "SortIndex", DefaultValue = "0")]
        public int SortIndex { get; set; } = 0;

        /// <summary>
        /// 是否启用
        /// </summary>
        [SugarColumn(ColumnName = "IsEnabled", DefaultValue = "1")]
        public bool IsEnabled { get; set; } = true;

        /// <summary>
        /// 是否显示
        /// </summary>
        [SugarColumn(ColumnName = "IsVisible", DefaultValue = "1")]
        public bool IsVisible { get; set; } = true;

        /// <summary>
        /// 菜单层级（0开始）
        /// </summary>
        [SugarColumn(ColumnName = "MenuLevel", DefaultValue = "0")]
        public int MenuLevel { get; set; } = 0;

        /// <summary>
        /// 完整路径（用于快速查询）
        /// </summary>
        [SugarColumn(ColumnName = "FullPath", Length = 500, IsNullable = true)]
        public string FullPath { get; set; }

        /// <summary>
        /// 菜单描述
        /// </summary>
        [SugarColumn(ColumnName = "Description", Length = 200, IsNullable = true)]
        public string? Description { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [SugarColumn(ColumnName = "CreatedTime", DefaultValue = "GETDATE()")]
        public DateTime CreatedTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 修改时间
        /// </summary>
        [SugarColumn(ColumnName = "UpdateTime", DefaultValue = "GETDATE()")]
        public DateTime UpdateTime { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// 菜单树形结构DTO
    /// </summary>
    public class MenuTreeDto : MenuModel
    {
        /// <summary>
        /// 子菜单列表
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public List<MenuTreeDto> Children { get; set; } = new List<MenuTreeDto>();

        /// <summary>
        /// 菜单类型名称
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public string MenuTypeName => MenuType switch
        {
            1 => "目录",
            2 => "菜单",
            3 => "按钮",
            _ => "未知"
        };

        /// <summary>
        /// 是否叶子节点
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public bool IsLeaf => Children == null || Children.Count == 0;
    }

    /// <summary>
    /// 菜单类型枚举
    /// </summary>
    public enum MenuType
    {
        /// <summary>
        /// 目录
        /// </summary>
        Directory = 1,

        /// <summary>
        /// 菜单
        /// </summary>
        Menu = 2,

        /// <summary>
        /// 按钮
        /// </summary>
        Button = 3
    }

    /// <summary>
    /// 动态路由DTO
    /// </summary>
    public class DynamicRouteDto
    {
        public long Id { get; set; }

        /// <summary>
        /// 路由路径
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// 父节点id
        /// </summary>
        public long? ParentMenuId { get; set; }

        /// <summary>
        /// 菜单编码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 组件名称
        /// </summary>
        public string Component { get; set; }

        /// <summary>
        /// 路由元信息
        /// </summary>
        public RouteMetaDto Meta { get; set; }

        /// <summary>
        /// 子路由
        /// </summary>
        public List<DynamicRouteDto> Children { get; set; } = new List<DynamicRouteDto>();
    }

    /// <summary>
    /// 路由元信息
    /// </summary>
    public class RouteMetaDto
    {
        /// <summary>
        /// 菜单标题
        /// </summary>
        public string Title { get; set; }

        

        /// <summary>
        /// 菜单图标
        /// </summary>
        public string Icon { get; set; }

        /// <summary>
        /// 是否显示
        /// </summary>
        public bool IsVisible { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; }

        /// <summary>
        /// 菜单类型
        /// </summary>
        public byte MenuType { get; set; }

        /// <summary>
        /// 排序序号
        /// </summary>
        public int SortIndex { get; set; }

        /// <summary>
        /// 菜单描述
        /// </summary>
        public string? Description { get; set; }
    }

    /// <summary>
    /// 修改或添加的参数
    /// </summary>
    public class MenuModelDto
    {
        public long Id { get; set; }
        public long ParentMenuId { get; set; }
        public string MenuName { get; set; }
        public string MenuCode { get; set; }
        public byte MenuType { get; set; }
        public string MenuPath { get; set; }
        public string MenuIcon { get; set; }
        public string ComponentName { get; set; }
        public int SortIndex { get; set; }
        public string Description { get; set; }
        public bool IsEnabled { get; set; } = true;
        public bool IsVisible { get; set; } = true;
    }

    /// <summary>
    /// 绑定菜单树
    /// </summary>
    public class MenuBandTreeDto
    {
        /// <summary>
        /// 菜单ID
        /// </summary>
        public long Id { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>
        public string MenuName { get; set; }

        /// <summary>
        /// 是否绑定（角色是否拥有该菜单）
        /// </summary>
        public bool IsBand { get; set; }

        /// <summary>
        /// 子菜单
        /// </summary>
        public List<MenuBandTreeDto> Children { get; set; } = new List<MenuBandTreeDto>();
    }
}